﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;

namespace TodoLister.Models
{
    /// <summary>
    /// 待办事项列表类
    /// </summary>
    public class TodoList : INotifyPropertyChanged
    {
        private ObservableCollection<TodoItem> _allItems;
        private ObservableCollection<TodoItem> _filteredItems;

        /// <summary>
        /// 待办事项集合
        /// </summary>
        public ObservableCollection<TodoItem> Items => _filteredItems;

        /// <summary>
        /// 全部待办事项数量
        /// </summary>
        public int TodoCount => _allItems.Count;

        /// <summary>
        /// 今日待办事项数量
        /// </summary>
        public int DailyCount => _allItems.Count(x => x.CreatedDate.Date == DateTime.Today);

        /// <summary>
        /// 已完成待办事项数量
        /// </summary>
        public int CompletedCount => _allItems.Count(x => x.IsCompleted);

        /// <summary>
        /// 属性更改事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 构造函数
        /// </summary>
        public TodoList()
        {
            _allItems = new ObservableCollection<TodoItem>();
            _filteredItems = new ObservableCollection<TodoItem>();
        }

        /// <summary>
        /// 应用过滤器
        /// </summary>
        public void ApplyFilter(TodoFilterType filterType)
        {
            _filteredItems.Clear();
            IEnumerable<TodoItem> filtered;

            switch (filterType)
            {
                case TodoFilterType.Todo:
                    filtered = _allItems;
                    break;
                case TodoFilterType.Daily:
                    filtered = _allItems.Where(x => x.CreatedDate.Date == DateTime.Today);
                    break;
                case TodoFilterType.Completed:
                    filtered = _allItems.Where(x => x.IsCompleted);
                    break;
                default:
                    filtered = _allItems;
                    break;
            }

            foreach (var item in filtered)
            {
                _filteredItems.Add(item);
            }

            // 通知属性变更
            OnPropertyChanged(nameof(TodoCount));
            OnPropertyChanged(nameof(DailyCount));
            OnPropertyChanged(nameof(CompletedCount));
        }

        /// <summary>
        /// 添加待办事项
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="priority">优先级</param>
        public void AddItem(string title, string content, TodoPriority priority = TodoPriority.None)
        {
            var item = new TodoItem(title, content) { Priority = priority };
            _allItems.Add(item);
            ApplyFilter(TodoFilterType.Todo);
        }

        /// <summary>
        /// 删除待办事项
        /// </summary>
        /// <param name="item">要删除的待办事项</param>
        public void RemoveItem(TodoItem item)
        {
            if (item != null)
            {
                _allItems.Remove(item);
                _filteredItems.Remove(item);
                // 通知属性变更
                OnPropertyChanged(nameof(TodoCount));
                OnPropertyChanged(nameof(DailyCount));
                OnPropertyChanged(nameof(CompletedCount));
            }
        }

        /// <summary>
        /// 更新待办事项
        /// </summary>
        /// <param name="item">要更新的待办事项</param>
        public void UpdateItem(TodoItem item)
        {
            var existingItem = _allItems.FirstOrDefault(i => i.Id == item.Id);
            if (existingItem != null)
            {
                var index = _allItems.IndexOf(existingItem);
                _allItems[index] = item;
            }
            ApplyFilter(TodoFilterType.Todo);
        }

        /// <summary>
        /// 设置待办事项优先级
        /// </summary>
        /// <param name="item">待办事项</param>
        /// <param name="priority">优先级</param>
        public void SetPriority(TodoItem item, TodoPriority priority)
        {
            var existingItem = _allItems.FirstOrDefault(i => i.Id == item.Id);
            if (existingItem != null)
            {
                existingItem.Priority = priority;
                OnPropertyChanged(nameof(TodoCount));
                OnPropertyChanged(nameof(DailyCount));
                OnPropertyChanged(nameof(CompletedCount));
            }
        }

        /// <summary>
        /// 清空列表
        /// </summary>
        public void Clear()
        {
            _allItems.Clear();
            _filteredItems.Clear();
            // 通知属性变更
            OnPropertyChanged(nameof(TodoCount));
            OnPropertyChanged(nameof(DailyCount));
            OnPropertyChanged(nameof(CompletedCount));
        }

        /// <summary>
        /// 触发属性更改通知
        /// </summary>
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
